package com.qf.fmall01.controller;

import com.qf.fmall01.entity.ShoppingCart;
import com.qf.fmall01.service.ShoppingCartService;
import com.qf.fmall01.utils.JwtUtils;
import com.qf.fmall01.vo.ResStatus;
import com.qf.fmall01.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("shopcart")
@CrossOrigin
public class ShopCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public ResultVO addShopCart(@RequestBody ShoppingCart shoppingCart){
        return shoppingCartService.addShopCart(shoppingCart);
    }

    @RequestMapping(value = "list",method = RequestMethod.GET)
    @ResponseBody
    public ResultVO list(Integer userId){
        return shoppingCartService.listByUserId(userId);
    }


    @RequestMapping(value = "update/{cid}/{cnum}",method = RequestMethod.PUT)
    @ResponseBody
    public ResultVO update(@PathVariable("cid") Integer cid, @PathVariable("cnum") Integer cnum){
        return shoppingCartService.update(cid,cnum);
    }


    @RequestMapping(value = "listbycids",method = RequestMethod.GET)
    @ResponseBody
    public ResultVO listbycids(String cids){
        return shoppingCartService.listbycids(cids);
    }


    @RequestMapping(value = "list2",method = RequestMethod.GET)
    @ResponseBody
    public ResultVO list2(String token){

        ResultVO vo = new ResultVO();
        if (token == null) {
            vo.setCode(ResStatus.NO);
            vo.setMsg("请提供合法的token");
        }else{
            boolean b = JwtUtils.parseToken(token);
            if (b){
                // 处理购物车的业务逻辑

                // 返回
                vo.setCode(ResStatus.OK);
                vo.setMsg("查询成功");
                vo.setData("购物车信息");
            }else{
                vo.setCode(ResStatus.NO);
                vo.setMsg("请提供合法的token");
            }
        }
        return vo;

    }


    @RequestMapping(value = "list1",method = RequestMethod.GET)
    @ResponseBody
    public ResultVO list1(){
        ResultVO vo = new ResultVO(ResStatus.OK,"list1 OK",null);
        return vo;

    }

}
